﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Xml;
using System.Xml.Xsl;
using System.IO;

namespace CurrentItemPropertiesWebPart.VisualWebPart1
{
    public partial class CurrentItemPropertiesUserControl : UserControl
    {
        protected Xml wyldesolutionsCurrentItemProperties;

        public CurrentItemPropertiesVisualWebPart WebPart { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (this.WebPart != null)
            {
                string sXml = GetCurrentItemXml(SPContext.Current.ListItem);
                wyldesolutionsCurrentItemProperties.DocumentContent = sXml;
                XslTransform transform = new XslTransform();
                string sTransformXml = string.Empty;
                try
                {
                    using (SPSite site = Helper.GetElevatedSite(SPContext.Current.Site))
                    {
                        using (SPWeb elevatedWeb = site.OpenWeb())
                        {
                            sTransformXml = elevatedWeb.GetFileAsString(SPContext.Current.Site.Url + (string.IsNullOrEmpty(this.WebPart.StyleLibraryUrl) ? "/VG Styles" : this.WebPart.StyleLibraryUrl) + "/" + (string.IsNullOrEmpty(this.WebPart.StyleFileName) ? "WS.CurrentItemProperties.xslt" : this.WebPart.StyleFileName));

                            transform.Load(new XmlTextReader(new StringReader(sTransformXml)));
                            wyldesolutionsCurrentItemProperties.Transform = transform;
                            wyldesolutionsCurrentItemProperties.Visible = true;
                        }
                    }
                }
                catch
                {
                    sTransformXml = "<Error>You do not have access to the library: " + (string.IsNullOrEmpty(this.WebPart.StyleLibraryUrl) ? "/VG Styles" : this.WebPart.StyleLibraryUrl) + " or it doesn't exist</Error>";
                }
            }
        }

        private string GetCurrentItemXml(SPListItem item)
        {
            string sResult = "<Item/>";
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(sResult);
            XmlElement xElem = xDoc.DocumentElement;
            foreach (SPField oField in item.Fields)
            {

                if (oField != null)
                {
                    try
                    {

                        string sValue = string.Empty;
                        XmlElement xNode = xDoc.CreateElement(oField.InternalName);
                        try
                        {
                            sValue = item[oField.Title].ToString();
                            if (oField.Type == SPFieldType.DateTime)
                            {
                                DateTime dtValue = Convert.ToDateTime(sValue);
                                sValue = dtValue.ToString("dd/MM/yyyy");
                            }
                        }
                        catch
                        {
                        }
                           
                        xNode.InnerText = sValue;
                        xElem.AppendChild(xNode);
                            
                    }
                    catch
                    {

                    }
                }
            }
            sResult = xDoc.OuterXml;
            return sResult;
        }
    }
}
